package com.cjy.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.cjy.pojo.User;
import com.cjy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private UserService userService;


    @GetMapping("/toRegister")
    public String toRegister(){
        return "/register";
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping
    @ResponseBody
    public String register(User user, @RequestParam("checkNumber") String checkNumber) {
        return userService.register(user, checkNumber);
    }

    /**
     * 检验账户是否存在
     * @param account
     * @return
     */
    @GetMapping("/checkAccount")
    @ResponseBody
    public String checkAccount(@RequestParam("account") String account) {
        if (StrUtil.isBlank(account)) {
            return "账号不能为空";
        }
        return userService.checkAcount(account);
    }

    /**
     * 检验邮箱
     * @param email
     * @return
     */
    @GetMapping("/checkEmail")
    @ResponseBody
    public String checkEmail(@RequestParam("email") String email) {
        if (StrUtil.isBlank(email)) {
            return "邮箱不能为空";
        }
        if (!Validator.isEmail(email)) {
            return "邮箱格式不对";
        }
        return userService.checkEmail(email);
    }

    /**
     * 发送验证码
     * @param email
     * @return
     */
    @PostMapping("/sendEmailNumber")
    @ResponseBody
    public String sendEmail(@RequestParam("email") String email) {
        if (StrUtil.isBlank(email)) {
            return "邮箱不能为空";
        }
        if (!Validator.isEmail(email)) {
            return "邮箱格式不对";
        }
        String result = userService.sendCheckNumber(email);
        return result;
    }
}
